package com.weijing.pretty_app.dialog

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weijing.pretty_app.databinding.DialogRestDevicesBinding
import com.weijing.pretty_app.util.KeyUtil
import com.weijing.pretty_app.util.LoadingAnimationHelper

class RestDevicesDialog(
    private val fangXiang: String, private val clickListener: SetRestClickListener
) : DialogFragment() {

    private lateinit var binding: DialogRestDevicesBinding
    private var loadingHelper: LoadingAnimationHelper? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,
    ): View {
        binding = DialogRestDevicesBinding.inflate(inflater, container, false)
        loadingHelper = LoadingAnimationHelper(binding.dialogTvLoading)

        binding.dialogTvRest.text = fangXiang

        binding.dialogIvClose.setOnClickListener {
            dismiss()
        }

        binding.dialogLlRest.setOnClickListener {
            clickListener.onClickListener()
            binding.dialogTvLoading.visibility = View.VISIBLE
            //开始加载动画 ...
            loadingHelper?.startLoading()
        }

        dialog?.window?.setBackgroundDrawable(ColorDrawable(resources.getColor(android.R.color.transparent)))

        LiveEventBus.get<Boolean>(KeyUtil.RESET_DEVICES).observeSticky(this, { mFlag ->
            if (mFlag) {
                loadingHelper?.stopLoading()
                binding.dialogTvLoading.visibility = View.GONE
                binding.dialogTvRestoreOk.visibility = View.VISIBLE
            }
        })
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        isCancelable = true
        val dm = DisplayMetrics()
        this.activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        this.dialog?.window?.setLayout(dm.widthPixels, dm.heightPixels)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        loadingHelper?.stopLoading()
    }

    interface SetRestClickListener {
        fun onClickListener()
    }
}

open class RestShowDialog {

    fun showing(
        context: FragmentManager, fangXiang: String,
        setListener: RestDevicesDialog.SetRestClickListener
    ) {
        val signSuccessDialog = RestDevicesDialog(fangXiang, setListener)
        if (!signSuccessDialog.isVisible && !signSuccessDialog.isAdded) {
            signSuccessDialog.show(context, "RestDevicesDialog")
        }
    }

    fun closeDialog(context: FragmentManager) {
        val fragment = context.findFragmentByTag("RestDevicesDialog")
        if (fragment != null) {
            val dialogFragment = fragment as DialogFragment
            dialogFragment.dismiss()
        }
    }
}